<template>
	<view class="content">
	<!-- 	<view>
			<image class="headicon" :src="userinfo.headicon" mode=""></image>
		</view> -->
		<uni-forms ref="form" :model="user" :rules="rules" class="uni-forms">
			<uni-forms-item label="用户名" name="name">
				<uni-easyinput prefix-icon="person" style="width: 460rpx;" v-model="user.name"
					placeholder="请输入用户名" />
			</uni-forms-item>
			<uni-forms-item label="密码" name="password">
				<uni-easyinput type="password" style="width: 460rpx; " prefix-icon="locked" v-model="user.password"
					placeholder="请输入密码" />
			</uni-forms-item>
			<uni-forms-item label="" name="">
				<button @click="login" style="width: 600rpx;" type="primary">登录</button>
				<button @click="toregister" style="width: 600rpx; margin-top: 20rpx;" type="primary" >注册</button>
			</uni-forms-item>
		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userinfo: {
					headicon: '/static/head_icon.png'
				},
				user: {
					name: '',
					password: ''
				}
			}
		},
		methods: {
			toregister(){
				uni.redirectTo({
					url:'../register/register'
				})
			},
			login(e) {
				uni.request({
					url: 'http://localhost:8090/freshmenapp/user/login',
					method: 'POST',
					data: this.user,
					success: res => {
						// console.log(res);
						if (res.data.success == true) {
							console.log(res);
							uni.showToast({
								title: res.data.data.name,
								icon: 'success'
							})
							uni.setStorage({
								key:'userinfo',
								data:res.data.data
							})
							uni.switchTab({
								url:'../news/news'
							})
						} else {
							uni.showToast({
								title: '用户名密码错误',
								icon: 'error'
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '网络错误',
							icon: 'error'
						});
					},
					complete: () => {}
				});
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		width: 100%;
		height: 100%;
	}

	.headicon {
		width: 150rpx;
		height: 150rpx;
		border-radius: 50%;
		margin: 50rpx;
	}
	.uni-forms{
		margin-top: 50rpx;
	}
	text {
		display: inline;
	}
</style>